public class MyQueue {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode first;
    public ListNode last;

    // 入队操作： 尾插法
    public void offer(int val) {
        ListNode node = new ListNode(val);
        if (first == null) {
            first = last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    // 获取队头元素删除
    public int poll() {
        if(first == null){
            return -1;
        }
        int pollVal = first.val;
        //只有一个节点
        if(first == last){
            first = null;
            last = null;
        }else{
            first = first.next;
            first.prev = null;
        }
        return pollVal;
    }

    // 获取队头元素不删除
    public int peek(){
        if(first == null){
            return -1;
        }
        return first.val;
    }

    // 相当于求链表的长度
    public int size(){
        ListNode cur = first;
        int count = 0;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean empty(){
        if(first == null){
            return true;
        }
        return false;
    }
}